<template>
	<view>
		<!-- 地址 -->
		<view class="address-section" @click="addAddress()">
			<view class="order-content">
				<text class="yticon icon-shouhuodizhi"></text>
				<view class="cen" v-if="addressData.location">
					<view class="top">
						<text class="name">{{addressData.name}}</text>
						<text class="mobile">{{addressData.cellphone}}</text>
					</view>
					<text class="address">{{addressData.location}}
					<block v-if="addressData.address">({{addressData.address}})</block>
					{{addressData.house}}</text>
				</view>
				<view class="cen" v-else>
					<text class="text-left padding-left">选择地址</text>
				</view>
				<text class="yticon icon-you"></text>
			</view>
			<image class="a-bg" src=""></image>
		</view>

		<view class="goods-section">
			<!-- 商品列表 -->
			<view class="g-item padding-top-sm" v-for="(item,index) in goodList" :key="index">
				<image :src="item.img | smallImage" lazy-load></image>
				<view class="right">
					<text class="title clamp">{{item.name}}</text>
					<text class="spec">{{item.specification}}</text>
					<view class="price-box">
						<text class="price">￥{{item.price}}</text>
						<text class="number">x {{item.number}}</text>
					</view>
				</view>
			</view>
		</view>
		<!-- 金额明细 -->
		<view class="yt-list">
			<view class="yt-list-cell b-b">
				<text class="cell-tit clamp">商品金额</text>
				<text class="cell-tip">￥{{total | 1000}}</text>
			</view>
			<view class="yt-list-cell b-b">
				<text class="cell-tit clamp">运费</text>
				<text class="cell-tip">
					<block v-if="addressData">
						<block v-if="carriage > 0">
							{{carriage | 1000}}
						</block>
						<block v-else>
							免运费
						</block>
					</block>
					<block v-else>
						请选择收货地址
					</block>
				</text>
			</view>
			<view class="yt-list-cell desc-cell">
				<text class="cell-tit clamp">备注</text>
				<input class="desc" type="text" v-model="data.remark" placeholder="请填写备注信息" placeholder-class="placeholder" maxlength="200"/>
			</view>
		</view>

		<!-- 底部 -->
		<view class="footer">
			<view class="price-content">
				<text>实付款</text>
				<text class="price-tip">￥</text>
				<text class="price">{{outPocket | 1000}}</text>
			</view>
			<text class="submit" @click="submit">提交订单</text>
		</view>
	</view>
</template>

<script>
	import Shipping from '../../api/shipping'
	import GoodIndent from '../../api/goodIndent'
	import {mapMutations} from 'vuex'
	export default {
		data() {
			return {
				maskState: 0, //优惠券面板显示状态
				desc: '', //备注
				payType: 1, //1微信 2支付宝
				couponList: [
					{
						title: '新用户专享优惠券',
						price: 5,
					},
					{
						title: '庆五一发一波优惠券',
						price: 10,
					},
					{
						title: '优惠券优惠券优惠券优惠券',
						price: 15,
					}
				],
				goodList: [],
				addressData: {},
				carriage: 0,
				total: 0,
				outPocket: 0,
				data: {
					indentCommodity: [],
					address: {},
					remark: '',
					carriage: 0
				}
			}
		},
		onLoad(option){
			this.loginCheck()
			this.loadData()
		},
		methods: {
			...mapMutations(['loginCheck']),
			//商品数据
			async loadData(){
				this.cartList = []
				this.invalidGood = []
				let cartList =  uni.getStorageSync('dsshopOrderList') || []
				const that = this
				for(var k in cartList){
					cartList[k].checked = true
					cartList[k].loaded = 'loaded'
					if(cartList[k].good_sku){
						cartList[k].good_sku.skus.forEach(item=>{
							if(cartList[k].specification){
								cartList[k].specification+= item.v + ';'
							}else{
								cartList[k].specification = item.v + ';'
							}

						})
						cartList[k].specification = cartList[k].specification.substr(0,cartList[k].specification.length-1)
					}
					if(cartList[k].invalid === true){ //失效的商品
						cartList.splice(k,1)
					}
				}
				this.goodList = cartList
				that.data.indentCommodity = cartList
				that.calcTotal()  //计算总价
				that.getOne()


			},
			//获取默认收货地址
			getOne(){
				const that = this
				Shipping.freight(0,this.goodList, function(res){
					that.addressData = res.shipping ? res.shipping : ''
					that.carriage = res.carriage ? res.carriage : 0
					that.outPocketTotal() //实付金额
				})
			},
			numberChange(data) {
				this.number = data.number;
			},
			changePayType(type){
				this.payType = type;
			},
			submit(){
				if(!this.addressData){
					this.$api.msg('请选择地址')
					return false
				}
				this.data.address = this.addressData
				this.data.carriage = this.carriage
				GoodIndent.create(this.data,function(res){
					//比对购物车, 清除已下单的商品
					const cartList  =  uni.getStorageSync('dsshopCartList') || {}
					for(var i=0;i<cartList.length;i++){
						if(cartList[i].checked){
						   cartList.splice(i--, 1);
						}
					}
					uni.setStorageSync('dsshopCartList', cartList)
					GoodIndent.addShoppingCart(cartList,function(res){})
					//清除购买列表
					uni.removeStorageSync('dsshopOrderList')
					uni.redirectTo({
						url: '/pages/money/pay?id='+res
					})
				})

			},
			addAddress(){
				uni.navigateTo({
					url: `/pages/address/address?type=1`
				})
			},
			//地址选择回调
			refreshAddress(item){
				const that = this
				Shipping.freight(item.id,this.goodList, function(res){
					that.carriage = res.carriage ? res.carriage : 0
					that.outPocketTotal() //实付金额
				})
				this.addressData = item
			},
			//计算总价
			calcTotal(){
				let list = this.goodList
				let total = 0
				for(var k in list){
					total += list[k].price * list[k].number
				}
				this.total = Number(total.toFixed(2))
			},
			//计算实付金额
			outPocketTotal(){
				let outPocket = 0
				outPocket = outPocket + this.total + this.carriage
				this.outPocket = Number(outPocket.toFixed(2))
			},
			stopPrevent(){}
		}
	}
</script>

<style lang="scss">
	page {
		background: $page-color-base;
		padding-bottom: 100upx;
	}

	.address-section {
		padding: 30upx 0;
		background: #fff;
		position: relative;

		.order-content {
			display: flex;
			align-items: center;
		}

		.icon-shouhuodizhi {
			flex-shrink: 0;
			display: flex;
			align-items: center;
			justify-content: center;
			width: 90upx;
			color: #888;
			font-size: 44upx;
		}

		.cen {
			display: flex;
			flex-direction: column;
			flex: 1;
			font-size: 28upx;
			color: $font-color-dark;
		}

		.name {
			font-size: 34upx;
			margin-right: 24upx;
		}

		.address {
			margin-top: 16upx;
			margin-right: 20upx;
			color: $font-color-light;
		}

		.icon-you {
			font-size: 32upx;
			color: $font-color-light;
			margin-right: 30upx;
		}

		.a-bg {
			position: absolute;
			left: 0;
			bottom: 0;
			display: block;
			width: 100%;
			height: 5upx;
		}
	}

	.goods-section {
		margin-top: 16upx;
		background: #fff;
		padding-bottom: 1px;

		.g-header {
			display: flex;
			align-items: center;
			height: 84upx;
			padding: 0 30upx;
			position: relative;
		}

		.logo {
			display: block;
			width: 50upx;
			height: 50upx;
			border-radius: 100px;
		}

		.name {
			font-size: 30upx;
			color: $font-color-base;
			margin-left: 24upx;
		}

		.g-item {
			display: flex;
			margin: 20upx 30upx;

			image {
				flex-shrink: 0;
				display: block;
				width: 140upx;
				height: 140upx;
				border-radius: 4upx;
			}

			.right {
				flex: 1;
				padding-left: 24upx;
				overflow: hidden;
			}

			.title {
				font-size: 30upx;
				color: $font-color-dark;
			}

			.spec {
				font-size: 26upx;
				color: $font-color-light;
			}

			.price-box {
				display: flex;
				align-items: center;
				font-size: 32upx;
				color: $font-color-dark;
				padding-top: 10upx;

				.price {
					margin-bottom: 4upx;
				}
				.number{
					font-size: 26upx;
					color: $font-color-base;
					margin-left: 20upx;
				}
			}

			.step-box {
				position: relative;
			}
		}
	}
	.yt-list {
		margin-top: 16upx;
		background: #fff;
	}

	.yt-list-cell {
		display: flex;
		align-items: center;
		padding: 10upx 30upx 10upx 40upx;
		line-height: 70upx;
		position: relative;

		&.cell-hover {
			background: #fafafa;
		}

		&.b-b:after {
			left: 30upx;
		}

		.cell-icon {
			height: 32upx;
			width: 32upx;
			font-size: 22upx;
			color: #fff;
			text-align: center;
			line-height: 32upx;
			background: #f85e52;
			border-radius: 4upx;
			margin-right: 12upx;

			&.hb {
				background: #ffaa0e;
			}

			&.lpk {
				background: #3ab54a;
			}

		}

		.cell-more {
			align-self: center;
			font-size: 24upx;
			color: $font-color-light;
			margin-left: 8upx;
			margin-right: -10upx;
		}

		.cell-tit {
			flex: 1;
			font-size: 26upx;
			color: $font-color-light;
			margin-right: 10upx;
		}

		.cell-tip {
			font-size: 26upx;
			color: $font-color-dark;

			&.disabled {
				color: $font-color-light;
			}

			&.active {
				color: $base-color;
			}
			&.red{
				color: $base-color;
			}
		}

		&.desc-cell {
			.cell-tit {
				max-width: 90upx;
			}
		}

		.desc {
			flex: 1;
			font-size: $font-base;
			color: $font-color-dark;
		}
	}

	/* 支付列表 */
	.pay-list{
		padding-left: 40upx;
		margin-top: 16upx;
		background: #fff;
		.pay-item{
			display: flex;
			align-items: center;
			padding-right: 20upx;
			line-height: 1;
			height: 110upx;
			position: relative;
		}
		.icon-weixinzhifu{
			width: 80upx;
			font-size: 40upx;
			color: #6BCC03;
		}
		.icon-alipay{
			width: 80upx;
			font-size: 40upx;
			color: #06B4FD;
		}
		.icon-xuanzhong2{
			display: flex;
			align-items: center;
			justify-content: center;
			width: 60upx;
			height: 60upx;
			font-size: 40upx;
			color: $base-color;
		}
		.tit{
			font-size: 32upx;
			color: $font-color-dark;
			flex: 1;
		}
	}

	.footer{
		position: fixed;
		left: 0;
		bottom: 0;
		z-index: 995;
		display: flex;
		align-items: center;
		width: 100%;
		height: 90upx;
		justify-content: space-between;
		font-size: 30upx;
		background-color: #fff;
		z-index: 998;
		color: $font-color-base;
		box-shadow: 0 -1px 5px rgba(0,0,0,.1);
		.price-content{
			padding-left: 30upx;
		}
		.price-tip{
			color: $base-color;
			margin-left: 8upx;
		}
		.price{
			font-size: 36upx;
			color: $base-color;
		}
		.submit{
			display:flex;
			align-items:center;
			justify-content: center;
			width: 280upx;
			height: 100%;
			color: #fff;
			font-size: 32upx;
			background-color: $base-color;
		}
	}
</style>
